package com.situ.yB2C.cart.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.yB2C.cart.dao.CartDao;
import com.situ.yB2C.cart.pojo.Cart;
import com.situ.yB2C.cart.service.CartService;
import com.situ.yB2C.util.ContextUtil;
@Service
public class CartServiceImpl implements CartService {
	@Autowired
	private CartDao cartDao;
	
	@Override
	public int doaddCart(Cart cart, Long userId, HttpSession session) {
		String createBy=ContextUtil.getBuyerUserCode(session);
		//LOG.debug("=============================="+createBy);
		//将购物车的数据 (保存|更新) 到数据库中
		Long productId= cart.getProductId();
		Cart oldCart=cartDao.selectOnByUserAndPro(userId,productId);
		if(oldCart !=null ) {
			//相同的商品购买是递增的
			oldCart.setOrderCount(oldCart.getOrderCount()+cart.getOrderCount());
			oldCart.setUpdateBy(createBy);
			oldCart.setUpdateDate(new Date());
			cartDao.update(oldCart);
		}else {
			cart.setUserId(userId);
			cart.setActiveFlag(1);
			cart.setCreateBy(createBy);
			cart.setCreateDate(new Date());
			cartDao.insert(cart);
		}
		//更新放置在session中的购物车的数据
		List<Cart> cartList=cartDao.selectAllByUserId(userId);
		session.setAttribute(ContextUtil.SESSION_LOGIN_BUYER, cartList);
		return 1;
	}

}
